//app.js
App({
  onLaunch: function () {
    let _this = this;
    // let loginStatus;
    wx.login({
      success: res => {
        var code = res.code;
        console.log('code',code);
       
        wx.getSetting({
          success: res => {
            // if(){

            // }
            // console.log('code---res',res.authSetting);
            // let authSetting = res.authSetting;
            // if (authSetting['scope.userInfo']==false){



              
            //   // let loginStatus = true;
            //   // this.getPromission(loginStatus);

            // }
              wx.getUserInfo({
                success: res => {
                  console.log('---res', res)
                  var session = {
                    url: _this.globalData.baseUrl + '/api/user/login',
                    data: {
                      code: code,
                      iv: res.iv,
                      encryptedData: res.encryptedData
                    },
                    method: 'POST',
                    success: function (res) {
                      wx.hideLoading();
                      if (!res.data.data.session_key) {
                        _this.wxrequest(session)
                      } else {
                        wx.setStorage({
                          key: 'session_key',
                          data: res.data.data.session_key
                        });
                      }

                    }
                  }

                  _this.wxrequest(session)

                }
              })
            }
        })

      }
    })
  
  },
  getPromission: function (loginStatus) {
    if (!loginStatus) {
      wx.openSetting({
        success: function (data) {
          if (data) {
            if (data.authSetting["scope.userInfo"] == true) {
              loginStatus = true;
              wx.getUserInfo({
                withCredentials: false,
                success: function (data) {
                  console.info("2成功获取用户返回数据");
                  console.info(data.userInfo);
                  wx.setStorageSync('loginAgain', false)
                },
                fail: function () {
                  console.info("2授权失败返回数据");
                }                });
            }
          } 
        },
        fail: function () {
          console.info("设置失败返回数据");
        }        });
    } else {
      wx.login({
        success: function (res) {
          if (res.code) {
                         wx.getUserInfo({
              withCredentials: false,
              success: function (data) {
                console.info("1成功获取用户返回数据");
                console.info(data.userInfo);
              },
              fail: function () {
                console.info("1授权失败返回数据");
                loginStatus = false;
                // 显示提示弹窗
                wx.showModal({
                  title: '授权',
                  content: '请授权个人信息',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                      wx.openSetting({
                        success: function (data) {
                          if (data) {
                            if (data.authSetting["scope.userInfo"] == true) {
                              loginStatus = true;
                              wx.getUserInfo({
                                withCredentials: false,
                                success: function (data) {
                                  console.info("3成功获取用户返回数据");
                                  console.info(data.userInfo);
                                },
                                fail: function () {
                                  console.info("3授权失败返回数据");
                                }
                              });
                            }
                          }
                        },
                        fail: function () {
                          console.info("设置失败返回数据");
                        }
                      });

                    } else if (res.cancel) {
                      wx.openSetting({
                        success: function (data) {
                          if (data) {
                            if (data.authSetting["scope.userInfo"] == true) {
                              loginStatus = true;
                              wx.getUserInfo({
                                withCredentials: false,
                                success: function (data) {
                                  console.info("3成功获取用户返回数据");
                                  console.info(data.userInfo);
                                },
                                fail: function () {
                                  console.info("3授权失败返回数据");
                                }                                });
                            }
                          } 
                        },
                        fail: function () {
                          console.info("设置失败返回数据");
                        }                        });
                    }
                  }
                });
              }              });
          }
        },
        fail: function () {
          console.info("登录失败返回数据");
        }       });
    }
  },
  // 请求数据
  wxrequest(obj) {
    let that = this
    wx.showLoading({
      title: '请求数据中...'
      // mask: true
    })
    wx.request({
      url: obj.url || useUrl.login,
      method: obj.method || 'POST',
      data: obj.data || {},
      header: {
        'content-type': obj.header || 'application/x-www-form-urlencoded'
      },
      success: obj.success || function () {
        console.log('未传入success回调函数')
      },
      fail: obj.fail || function (err) {
        console.log('未传入fail回调函数,err:' + err.errMsg)
      },
      complete: obj.complete || function (res) {
        if (res.data.code === 401) {
          setTimeout(() => {
            wx.login({
              success(res) {

                if (res.code) {
                  wx.getUserInfo({
                    lang: 'zh_CN',
                    success(res2) {
                      that.wxrequest({
                        url: that.globalData.baseUrl +'/api/user/login',
                        data: {
                          code: res.code,
                          iv: res2.iv,
                          encryptedData: res2.encryptedData
                        },
                        success(res3) {
                          wx.setStorageSync('session_key', res3.data.data.session_key)
                          obj.data.session_key = that.gs()
                          console.log('res3 get in')
                          that.wxrequest(obj)
                        }
                      })
                    },
                    fail(err) {
                      wx.showToast({
                        title: '用户拒绝授权'
                      })
                    }
                  })
                } else {
                  wx.showToast({
                    title: '请删除小程序后，重新打开并授权'
                  })
                }
              }
            })
          }, 300)
        }
      }
    })
  },
   // 房间信息请求数据
  houseCate(){

  },
  // 获取缓存
  gs (key) {
    return wx.getStorageSync(key || 'session_key')
  },
  globalData: {
    userInfo: null,
    baseUrl:'https://ktv.fwhtz.net',
    acount:0
  }
})